package com.cs543.j.client.cloudService.storage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Inet4Address;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Vector;

import android.util.Base64;
import android.util.Log;

import com.cs543.j.client.cloudService.Client;
import com.cs543.j.client.cloudService.CloudRequestCode;
import com.cs543.j.client.cloudService.RemoteClient;
import com.cs543.j.client.cloudService.filesystem.Directory;
import com.cs543.j.client.cloudService.filesystem.Entry;
import com.cs543.j.client.cloudService.filesystem.File;
import com.cs543.j.client.comm.RemoteCloudCommunication;

public class CloudStorage extends Storage {

	public void lookup(Directory directory, OnLookupResponceListener listener) {
		List parameter = new Vector();
		parameter.add(directory.getPath());
		
		Log.i("CloudStorage", "Connect to lookup : " + this.getAddress() );
		
		SendRPCMessage rpc = new SendRPCMessage(this.getAddress(), CloudRequestCode.LOOKUP, parameter, 0, new JSON2Entries(
				directory, listener));
		rpc.start();		
	}

	public void download(File remoteFile, java.io.File targetLocation) {
		List parameter = new Vector();
		parameter.add(remoteFile.getPath());
		SendRPCMessage rpc = new SendRPCMessage(this.getAddress(), CloudRequestCode.READ, parameter, 0, new JSON2File(
				targetLocation));
		rpc.start();
	}
	
	public void upload(Directory remoteLocation, java.io.File originalFile)
	{
		List parameter = new Vector();
		
		if(remoteLocation.isRootDirectory())
			parameter.add("/" + originalFile.getName() );
		else
			parameter.add(remoteLocation.getPath() + "/" + originalFile.getName() );
		
		Log.i("CloudStorage","Upload Location : " + parameter.get(0));
		
		StringBuffer result = new StringBuffer();
		
		FileInputStream fis;
		try {
			fis = new FileInputStream(originalFile);
			byte[] buffer = new byte[(int) originalFile.length()];
			
			fis.read(buffer);
			result.append(Base64.encodeToString(buffer, Base64.URL_SAFE ));
			fis.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		parameter.add(result.toString());
		
		SendRPCMessage rpc = new SendRPCMessage(this.getAddress(), CloudRequestCode.UPLOAD, parameter, 0, null);
		rpc.start();
	}
	
	public void copy(File originalFile, String targetClient, String remoteFile )
	{
		List parameter = new Vector();
		parameter.add(originalFile.getPath() );
		parameter.add( targetClient  );
		parameter.add( remoteFile );
		
		SendRPCMessage rpc = new SendRPCMessage(this.getAddress(), CloudRequestCode.COPY, parameter, 0, null);
		rpc.start();
	}
	
	public void delete(Entry targetFile)
	{
		List parameter = new Vector();
		parameter.add(targetFile.getPath() );
		
		SendRPCMessage rpc = new SendRPCMessage(this.getAddress(), CloudRequestCode.DELETE, parameter, 0, null);
		rpc.start();
	}
	
	public void move(Entry originalFile, Entry targetFile)
	{
		List parameter = new Vector();
		parameter.add(originalFile.getPath() );
		parameter.add(targetFile.getPath() );
		
		SendRPCMessage rpc = new SendRPCMessage(this.getAddress(), CloudRequestCode.MOVE, parameter, 0, null);
		rpc.start();
	}

	public CloudStorage(RemoteClient client) {
		super(client);
	}
	
	public RemoteClient getClient()
	{
		return ((RemoteClient) super.getClient());		
	}

	private String getAddress() {
		return this.getClient().getUrl();
	}

}
